﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Portal.Environment;
using Portal.Language;
using Portal.Interface;

public partial class ConfigForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Portal.Environment.Environments.SetEnvironment();
        Session["ActiveTheme"] = Environments.GetAppSettings("ActiveTheme");
        LoadWebPart();
        LoadCurrentUserInfo();
    }

    private void LoadCurrentUserInfo()
    {
        string Display = "guest";
        if (Environments.CurrentLoginUser != null)
        {
            Display = Environments.CurrentLoginUser.LoginName + " / " + Environments.CurrentLoginUser.FullName;
        }
        placeHolderLoginUser.Controls.Add(new System.Web.UI.LiteralControl("Login: " + Display));
    }


    private void LoadWebPart()
    {
        try
        {
            if (Portal.Environment.Environments.CheckSessionTimeOut(false))
            {
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText("Home", "SessionTimeOut", "Session timeout! Xin vui lòng đăng nhập lại") + "&nbsp;<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText("Home","CloseText","Đóng") + "\">"));
                return;
            }
            string WebPartId = Request["WebPartId"];
            if (WebPartId == null)
            {
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText("Home","WebPartNotFound","Không tìm thấy webpart {WebPartId}")));
                return;
            }
            Portal.Interface.WebPartInt
                LoadedWebPartInt = Portal.Environment.WebPartUtilities.FindWebPart(WebPartId);
            if (LoadedWebPartInt == null)
            {
                string Msg =  ResourceBLL.GetText("Home","WebPartNotFound","Không tìm thấy webpart {WebPartId}");
                Msg = Msg.Replace("{WebPartId}", WebPartId);
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl("<div class=cssWebPartNotFound>" + Msg + "</div>"));
                return;
            }
            LoadParamCls loadParamCls = new LoadParamCls();
            if (LoadedWebPartInt.IsControl == false)
            {
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(LoadedWebPartInt.RenderWebPart(loadParamCls)));
            }
            else
            {
                string FileLoad = "UserControls/" + LoadedWebPartInt.WebPartId + "/" + LoadedWebPartInt.LoadUserControl;
                System.Web.UI.UserControl
                    LoadedWebUserControl = (System.Web.UI.UserControl)this.LoadControl(FileLoad);
                placeHolderContent.Controls.Add(LoadedWebUserControl);
            }
            LoadedWebPartInt.LoadSubControls(placeHolderContent, Page, loadParamCls);
        }
        catch (Exception ex)
        {
            placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(ex.Message.ToString()));
        }
    }
}
